<?php
/**
*
* Controller for the front end User maintenance
*
* @package	VirtueMart
* @subpackage User
* @author Oscar van Eijk
* @link http://www.virtuemart.net
* @copyright Copyright (c) 2004 - 2010 VirtueMart Team. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* VirtueMart is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* @version $Id: user.php 3310 2011-05-20 12:23:06Z Milbo $
*/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

// Load the controller framework
jimport('joomla.application.component.controller');

/**
 * VirtueMart Component Controller
 *
 * @package		VirtueMart
 */
class MvmControllerUser extends JController
{

	public function __construct()
	{
		parent::__construct();
	}

	/**
	 * The general user/account maintance view for editing userdata.
	 * It redirects automatically to the task register for anonymous users.
	 *
	 */
	public function User(){

		//We just setup a new task for non registered users
		$user =& JFactory::getUser();
		$doc =& JFactory::getDocument();
		$format =& $doc->getType();
		$view = $this->getView('user', $format);

		/* Add the default model */
		$this->addModelPath( JPATH_COMPONENT.DS.'models' );
		$view->setModel( $this->getModel( 'user', 'VirtuemartModel' ), true );
		$view->setModel( $this->getModel( 'vendor', 'VirtuemartModel' ), true );
		$view->setModel( $this->getModel( 'userfields', 'VirtuemartModel' ), true );
//		$view->setModel( $this->getModel( 'orders', 'VirtuemartModel' ), true );

		/* Set the layout */
		$view->setLayout('default');
		$cid = JRequest::getVar('cid',null);
		if(!isset($cid)) JRequest::setVar('cid', (int)0);
		
		$model = $this->getModel( 'user', 'VirtuemartModel' );
		$address = $model->getUserAddress($user->id);
		$view->assignRef('address', $address);
		
		if ( $user->id == 0) {
			$view->assignRef('error', JError::raiseWarning('SOME_CODE', JText::_('Please log in')));
		} 
		

		/* Display it all */
		$view->display();

	}

	/**
	 * This is the save function for the normal user edit.php layout.
	 * We use here directly the userModel store function, because this view is for registering also
	 * it redirects to the standard user view.
	 *
	 * @author Max Milbers
	 */
	function saveUser(){
		$this->addModelPath( JPATH_COMPONENT.DS.'models' );
		$format =& JFactory::getDocument()->getType();
		$view = $this->getView('user', $format);
		
		
		$view->setModel($model =  $this->getModel( 'user', 'VirtuemartModel' ), true);

//		$data = JRequest::get('post');
		$data = JRequest::get('get');
		
		// Store multiple selectlist entries as a ; separated string
		if (key_exists('vendor_accepted_currencies', $data) && is_array($data['vendor_accepted_currencies'])) {
		    $data['vendor_accepted_currencies'] = implode(',', $data['vendor_accepted_currencies']);
		}

		$data['vendor_store_name'] = JRequest::getVar('vendor_store_name','','post','STRING',JREQUEST_ALLOWHTML);
		$data['vendor_store_desc'] = JRequest::getVar('vendor_store_desc','','post','STRING',JREQUEST_ALLOWHTML);
		$data['vendor_terms_of_service'] = JRequest::getVar('vendor_terms_of_service','','post','STRING',JREQUEST_ALLOWHTML);


		$error = $model->store($data);
		if(!JError::isError($error))
		{
			$user =& JFactory::getUser();
			
			// MVM supports only BillTo right now
			$address = $model->getUserAddress($user->id);
			// this should set the BT to the user
			$model->saveAddresToCart($address[0]);
			
			$view->assignRef('address', $address);
			$view->assignRef('message', JText::_('COM_MVM_USER_SAVED_SUCCESFULY'));
			
		}
		else
		{
			//set the error so the phone knows that it's shit
			$view->assignRef('error', $error);
		}
		parent::display();
		

	}
	
	function login()
	{
		// Check for request forgeries
//		JRequest::checkToken('request') or jexit( 'Invalid Token' );
			$document =& JFactory::getDocument();
			$viewType	= $document->getType();
			$viewName	= JRequest::getCmd( 'view', 'user' );
			$view = $this->getView($viewName, $viewType);
		
		global $mainframe;

		if ($return = JRequest::getVar('return', '', 'method', 'base64')) {
			$return = base64_decode($return);
			if (!JURI::isInternal($return)) {
				$return = '';
			}
		}

		$options = array();
		$options['remember'] = JRequest::getBool('remember', false);
		$options['return'] = $return;

		$credentials = array();
		$credentials['username'] = JRequest::getVar('username', '', 'method', 'username');
		$credentials['password'] = JRequest::getString('passwd', '', 'method', JREQUEST_ALLOWRAW);

		//preform the login action
		$error = $mainframe->login($credentials, $options);


		if(!JError::isError($error))
		{
			$messageQueue = $mainframe->getMessageQueue();
			if ( count($messageQueue)){
				$view->assignRef('message', $messageQueue[0]['message']);
			} else {
				$user =& JFactory::getUser();
			
				$model = $this->getModel( 'user', 'VirtuemartModel' );
				// MVM supports only BillTo right now
				$address = $model->getUserAddress($user->id);
				// this should set the BT to the user
				$model->saveAddresToCart($address[0]);
				$view->assignRef('address', $address);
				$view->assignRef('message', JText::_('Login successful'));
			}
					
		}
		else
		{
			//set the error so the phone knows that it's shit
			$view->assignRef('error', $error);
		}
		parent::display();
	}
	
}
// No closing tag
